<script setup lang="ts">
import type { FormInstance, FormRules } from "element-plus";
import { reactive, ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import { useRouter } from 'vue-router'
import Vcode from "vue3-puzzle-vcode";

let router = useRouter()
const imgs = ref([
  "https://img1.baidu.com/it/u=3550955980,4041911386&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
  "https://pic.rmb.bdstatic.com/bjh/a57559753abfc2172255e5bb38eea7534635.jpeg@h_1280",
]);
let isshow = ref(false);
const isVcodePassed = ref(false); // 验证码是否通过
const formModel = reactive({
  user: "",
  pass: "",
  phone: "",
});
let formRules = reactive({
  user: [
    { required: true, message: "请输入账号" },
    { min: 4, max: 20, message: "账号长度需要在4到20位之间" },
  ],
  pass: [
    { required: true, message: "请输入密码" },
    { min: 4, max: 20, message: "密码长度需要在6到20位之间" },
  ],
  phone: [
    { required: true, message: "请输入手机号" },
    { min: 11, max: 11, message: "手机号长度需要在11位" },
  ]
});

let handlregister = async () => {
  let res = await axios.post("http://localhost:3000/regiuser", formModel);
  if (res.data.code == 200) {
    router.push('/login')
    ElMessage({
      message: "注册成功",
      type: "success",
    });
  } else {
    ElMessage.error('用户名已存在')
  }
}
let register = async () => {
  if (!isVcodePassed.value) {
    isshow.value = true;
    return;
  }
  // 验证码通过后，执行注册逻辑
  handlregister()
};


// 验证码成功回调
const onSuccess = () => {
  isVcodePassed.value = true; // 验证码通过
  isshow.value = false; // 隐藏验证码
  handlregister(); // 验证成功后执行注册逻辑
};

// 验证码失败回调
const onFail = () => {
  isshow.value = false; // 验证码验证失败后隐藏验证码
  isVcodePassed.value = false; // 重置验证状态
  ElMessage.error("验证失败");
};
</script>

<template>
  <div id="Login">
    <div class="img">
      <img src="https://img1.baidu.com/it/u=4152366307,3892585726&fm=253&fmt=auto&app=138&f=JPEG?w=987&h=800"
        class="tx" />
      <div class="login">
        <div class="box">
          <el-form ref="form" :rules="formRules" :model="formModel">
            <el-form-item prop="user">
              <el-input v-model="formModel.user" placeholder="请输入用户名" clearable />
            </el-form-item>
            <el-form-item prop="pass">
              <el-input v-model="formModel.pass" type="password" placeholder="请输入密码" show-password clearable />
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="formModel.phone" placeholder="请输入手机号" clearable />
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="router.push('/login')">返回登录页面</el-button>
        </div>
      </div>
    </div>
    <Vcode :show="isshow" :src="imgs" @success="onSuccess" @fail="onFail" />
  </div>

</template>

<style lang="scss" scoped>
#Login {
  width: 100%;
  height: 100%;
  background: linear-gradient(to right top, #ecf5ff, #c6e2ff);

  .img {
    width: 100%;
    height: 100vh;
    background-image: url("https://img1.baidu.com/it/u=3743680466,2149120321&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500");
    background-size: cover;
    background-position: center;

    .tx {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      position: absolute;
      top: 34%; // 垂直居中
      left: 50%; // 水平居中
      transform: translate(-50%, -50%); // 通过 translate 实现精确居中
      z-index: 1;
    }

    .login {
      width: 500px;
      height: 300px;
      background-color: rgba($color: #000000, $alpha: 0.5);
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;

      .box {
        width: 50%;
        height: 100px;
        margin: 0 auto;
        margin-top: 50px;
      }
    }
  }
}
</style>